package actor.controller
{
	import capricornus.actor.Actor;
	import capricornus.actor.attribute.ActorAttribute;
	import capricornus.actor.controller.ActorComponentController;
	import capricornus.actor.events.ActorAttributeEvent;
	
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	
	import res.DefaultResManager;
	
	public class DemonComponentController extends ActorComponentController
	{
		private var _defaultWidth:int;
		private var _blood:Bitmap;
		private var _rect:Bitmap;
		private var _container:Sprite;
		
		public function DemonComponentController(actor:Actor)
		{
			super(actor);
			init();
		}
		
		override public function destroy():void
		{
			super.destroy();
			_actor.attribute.removeEventListener( ActorAttributeEvent.HP_CHANGED, onHPChanged );
			_actor.attribute.removeEventListener( ActorAttributeEvent.MAX_HP_CHANGED, onHPChanged );
			_blood.bitmapData = null;
		}
		
		private function init():void
		{
			createBlood();
		}
		
		private function createBlood():void
		{
			_blood = new Bitmap( DefaultResManager.defaultBloodBar );
			_defaultWidth = _blood.width;

			_rect = new Bitmap( DefaultResManager.defaultBloodBarRect );
			
			_container = new Sprite();
			_container.addChild( _blood );
			_container.addChild( _rect );
			_container.mouseChildren = false;
			_container.mouseEnabled = false;
			
			_container.x = -int(_rect.width/2);
			
			_list.addItem( _container );
			
			_actor.attribute.addEventListener( ActorAttributeEvent.HP_CHANGED, onHPChanged, false, 0, true );
			_actor.attribute.addEventListener( ActorAttributeEvent.MAX_HP_CHANGED, onHPChanged, false, 0, true );
		}
		
		private function onHPChanged( e:ActorAttributeEvent ):void
		{
			var att:ActorAttribute = _actor.attribute;
			_blood.width = int((att.hp/att.maxHp) * _defaultWidth);
		}
	}
}